<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" @submit.native.prevent>
      <el-form-item label="负责楼栋">
        {{buildingName}}<span style="margin-right: 10px;"></span>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="输入房屋号，位置，业主" clearable @submit.native.prevent></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
        class="table">
      <el-table-column
          prop="id"
          header-align="center"
          align="center"
          width="70"
          label="房屋号">
      </el-table-column>
      <el-table-column
          prop="address"
          header-align="center"
          align="center"
          label="位置">
      </el-table-column>
      <el-table-column
          prop="ownerName"
          header-align="center"
          align="center"
          label="业主">
      </el-table-column>
      <el-table-column
          prop="fireExtinguisherStatus"
          header-align="center"
          align="center"
          label="灭火器状态">
      </el-table-column>
      <el-table-column
          prop="smokeDetectorStatus"
          header-align="center"
          align="center"
          label="烟雾检测器状态">
      </el-table-column>
      <el-table-column
          prop="passageUnblockStatus"
          header-align="center"
          align="center"
          label="通道疏通情况">
      </el-table-column>
      <el-table-column
          prop="gasStatus"
          header-align="center"
          align="center"
          label="燃气情况">
      </el-table-column>
      <el-table-column
          prop="electricity"
          header-align="center"
          align="center"
          label="电力系统情况">
      </el-table-column>
      <el-table-column
          prop="score"
          header-align="center"
          align="center"
          :formatter="formatScore"
          label="得分">
      </el-table-column>
      <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="110"
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="check(scope.row.id)">上传检查记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--手机端显示-->
    <div class="cards">
      <el-card
          class="card"
          v-for="data in dataList"
          :key="data.id"
          @click.native="check(data.id)">
        <div class="title">
          {{data.address}}
        </div>
        <span class="content">
          {{data.id}}号
        </span>
        <span class="content">
          {{data.ownerName}}
        </span>
        <span class="content">
          {{data.score.toFixed(2)}}分
        </span>
      </el-card>
    </div>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[8,10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from '../inspection/inspection-add.vue'

export default {
  data() {
    return {
      buildingName: '',
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 8,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    this.getBuildingName()
    this.getDataList()
  },
  methods: {
    //获取负责的楼栋
    getBuildingName(){
      let loginUser=JSON.parse(localStorage.getItem('user')) || {}
      this.$request.get('/house/building/getBuildingNameById/'+loginUser.buildingId).then(res=>{
        if(res.code===200){
          this.buildingName=res.data
        }
        else {
          this.$message.error(res.msg)
        }
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$request({
        url: '/house/house/getInspectorHouseList',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        }
      }).then((res) => {
        if (res && res.code === 200) {
          this.dataList = res.page.records
          this.totalPage = res.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //保留两位
    formatScore(row, column, cellValue) {
      return parseFloat(cellValue).toFixed(2);
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增
    check(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
  }
}
</script>

<style scoped>
.cards {
  display: none;
}

.card {
  margin: 0.5rem 0;
}

.title {
  font-size: 1rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
}

.content {
  margin: 0 0.2rem;
}

@media (max-width: 700px) {
  .cards {
    display: block;
  }
  .table {
    display: none;
  }
}
</style>
